<template>
	<view class="imt-audio" style="width: 100%;">
		<view class="audio-wrapper">
			<view class="audio-number">{{format(current)}}</view>
			<slider class="audio-slider" :activeColor="color" block-size="16" :value="current" :max="duration>0?duration:initDuration"
				@changing="seek=true,current=$event.detail.value" @change="audio.seek($event.detail.value)"></slider>
			<view class="audio-number">{{format(duration>0?duration:initDuration)}}</view>
			<view class="audio-control audio-control-switch" :style="{borderColor:color}"
				@click="audio.paused?play():audio.pause()">
				<text v-if="paused" class="cuIcon-playfill text-white"></text>
				<text v-else class="cuIcon-stop text-white"></text>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		name: "AudioPlayer",
		data() {
			return {
				audio: uni.createInnerAudioContext(),
				current: 0, //当前进度(s)
				duration: 0, //总时长(s)
				paused: true, //是否处于暂停状态
				loading: false, //是否处于读取状态
				seek: false //是否处于拖动状态
			}
		},
		props: {
			src: {
				type:String,
				default:null
			}, //音频链接
			autoplay: {
				type: Boolean,
				default: false
			}, //是否自动播放
			color: {
				type: String,
				default: '#fff'
			},//主色调
			initDuration:{
				type:Number,
				default:0
			}
		},
		methods: {
			//格式化时长
			format(num) {
				return '0'.repeat(2 - String(Math.floor(num / 60)).length) + Math.floor(num / 60) + ':' + '0'.repeat(2 -
					String(Math.floor(num % 60)).length) + Math.floor(num % 60)
			},
			//点击播放按钮
			play() {
				this.audio.play()
				this.loading = true
			}
		},
		created() {
			if (this.src) {
				this.audio.src = this.src
				this.autoplay && this.play()
			}
			this.audio.obeyMuteSwitch = false
			//音频进度更新事件
			this.audio.onTimeUpdate(() => {
				if (!this.seek) {
					this.current = this.audio.currentTime
				}
				if (!this.duration) {
					this.duration = this.audio.duration
					this.$emit('onGetDuration',this.duration)
				}
			})
			this.audio.onCanplay(()=>{
				if (!this.duration) {
					this.duration = this.audio.duration
					this.$emit('onGetDuration',this.duration)
				}
			})
			//音频播放事件
			this.audio.onPlay(() => {
				this.paused = false
				this.loading = false
			})
			//音频暂停事件
			this.audio.onPause(() => {
				this.paused = true
			})
			//音频结束事件
			this.audio.onEnded(() => {
				if (this.continue) {
					this.next()
				} else {
					this.paused = true
					this.current = 0
				}
			})
			//音频完成更改进度事件
			this.audio.onSeeked(() => {
				this.seek = false
			})
		},
		beforeDestroy() {
			this.audio.destroy()
		},
		watch: {
			src(src, old) {
				this.audio.src = src
				this.current = 0
				this.duration = 0
				if (this.autoplay) {
					this.play()
				}
			},
			initDuration(duration,old) {
				this.duration = duration
			}
		}
	}
</script>

<style>
	.imt-audio {
		padding: 20upx 0;
		background: #8799a3;
		border-radius: 20upx;
	}

	.audio-wrapper {
		display: flex;
		align-items: center;
	}

	.audio-number {
		width: 120upx;
		font-size: 24upx;
		line-height: 1;
		color: #FFF;
		text-align: center;
	}

	.audio-slider {
		flex: 1;
		margin: 0;
	}

	.audio-control-wrapper {
		margin-top: 20upx;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.audio-control {
		font-size: 32upx;
		line-height: 1;
		border: 4upx solid;
		border-radius: 50%;
		padding: 16upx;
	}

	.audio-control-switch {
		font-size: 30upx;
		margin-right: 30upx;
	}
	
</style>
